package com.bigmodel.controller;

import cn.hutool.core.lang.UUID;
import com.bigmodel.vo.RequestAmdbAttrVO;
import com.bigmodel.vo.ResponseResult;
import cn.hutool.core.util.RandomUtil;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/test")
public class TestController {

    /**
     * 数据库状态查询
     * @return
     */
    @GetMapping("/getDbStatus")
    public ResponseResult getDbStatus(@RequestParam String name) {
        long random = RandomUtil.randomLong(0, 2);
        if(random==0) {
            return new ResponseResult().setData("error");
        }else{
            return new ResponseResult().setData("success");
        }
    }

    /**
     * 数据库重启
     * @return
     */
    @GetMapping(value = "/restartDb")
    public ResponseResult restartDb(@RequestParam String name){
        String msg = name+"的数据库重启成功";
        return  new ResponseResult().setMsg(msg);
    }


    /**
     * 应用实例状态查询
     * @return
     */
    @GetMapping("/getInstanceStatus")
    public ResponseResult getInstanceStatus(@RequestParam String name) {
        long random = RandomUtil.randomLong(0, 2);
        if(random==0) {
            return new ResponseResult().setData("error");
        }else{
            return new ResponseResult().setData("success");
        }
    }

    /**
     * 应用实例重启
     * @return
     */
    @GetMapping(value = "/restartInstance")
    public ResponseResult restartInstance(@RequestParam String name){
        String msg = name+"应用实例重启成功";
        return  new ResponseResult().setMsg(msg);
    }





    /**
     * AMDB拓扑关系查询工具
     * @return
     */
    @GetMapping("/relationByName")
    public ResponseResult relationByName(@RequestParam String name) {

       List<String> list = new ArrayList<>();
       list.add("adminCenter");
       list.add("aiCenter");

       return  new ResponseResult().setData(list);
    }


    /**
     * 根据对象名称查对象amdbId
     * @return
     */
    @GetMapping("/findAmdbIdByName")
    public ResponseResult findAmdbIdByName(@RequestParam String name) {
        String amdbId = name.hashCode()+"";
        return  new ResponseResult().setData(amdbId);
    }

    @GetMapping("/findDiskRate")
    public ResponseResult findDiskRate(@RequestParam String objectName) {
        RandomUtil randomUtil = new RandomUtil();
        long rate = RandomUtil.randomLong(50 ,70);
        return  new ResponseResult().setData(rate+"%");
    }


    @GetMapping("/getAmdbIdByName")
    public ResponseResult getAmdbIdByName(@RequestParam String objectName) {
        String amdbId = objectName.hashCode()+"";
        return  new ResponseResult().setData(amdbId);
    }

    @PostMapping("/getAttrByAmdbId")
    public ResponseResult getAttrByAmdbId(@RequestBody RequestAmdbAttrVO amdbAttrVO) {
        String amdbId = amdbAttrVO.getAmdbId();
        Map<String,Object> attrMap = new HashMap<>();
        for(String attrName : amdbAttrVO.getAttrList()){
            if("ip".equals(attrName)
            ||"IP地址".equals(attrName)
            ){
                String hashCode = String.valueOf(amdbId.hashCode());
                String one = hashCode.substring(hashCode.length()-1);
                String two = hashCode.substring(hashCode.length()-2,hashCode.length()-1);
                attrMap.put("ip","192.168."+one+"."+two);
            }else if("userName".equals(attrName)
            ||"用户名".equals(attrName)
            ){
                attrMap.put("userName","root");
            }else if("passWord".equals(attrName)
            ||"密码".equals(attrName)
            ){
                attrMap.put("passWord","888888");
            }
        }
        return  new ResponseResult().setData(attrMap);
    }


    @GetMapping("/shellLogin")
    public ResponseResult shellLogin(@RequestParam String ip,
                                     @RequestParam String userName,
                                     @RequestParam String passWord) {
        Map<String,Object> map = new HashMap<>();
        UUID uuid = UUID.fastUUID();
        return  new ResponseResult().setData(uuid.toString().replaceAll("-","")).setMsg("登录成功");
    }

    @PostMapping("/deleteFile")
    public ResponseResult deleteFile(@RequestBody List<String> fileList) {
        Map<String,Object> map = new HashMap<>();
        UUID uuid = UUID.fastUUID();
        map.put("sessionId",uuid.toString().replaceAll("-",""));
        return  new ResponseResult().setData(map).setMsg("删除成功");
    }


    @GetMapping("/shellLogout")
    public ResponseResult shellLogout(@RequestParam String sessionId) {
        Map<String,Object> map = new HashMap<>();
        map.put("sessionId",sessionId);
        return  new ResponseResult().setData(map).setMsg("登出成功");
    }


    @GetMapping("/findLog")
    public ResponseResult findLog() {
        List<String> fileList = new ArrayList<>();
        fileList.add("/var/a/b/c/nginx.1.log");
        fileList.add("/var/a/b/c/nginx.2.log");
        fileList.add("/var/a/b/c/nginx.3.log");
        fileList.add("/var/a/b/c/tomcat.1.log");
        fileList.add("/var/a/b/c/tomcat.2.log");
        fileList.add("/var/a/b/c/tomcat.3.log");
        return  new ResponseResult().setData(fileList).setMsg("登出成功");
    }

}
